#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import jsonify, request
from flask_restful import Resource

from utility.api_security import APISecurity
from utility.odoo_manager import OdooManager  # 操作odoo的单例
from utility.error_code import error_code  # 异常代码
from utility.message_manager import MessageBusManager
from event import event_type
from command import command
from utility import tools
from utility.tools import nullablea_dict, check_physical, check_progress
import logging

FORMAT = "%(asctime)s %(thread)d %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)


class PhyExaNewAPI(Resource):
    """
    新增体格检查
    :request: post
    :param archivesId: 档案ID
    :param checkType: 类型 null 护士工作站 1 初检 2 常规 3 产后
    :param cg: 常规检查
            {   
                "cgTjrq": 体检日期
                "cgSg": 身高CM
                "cgTz": 体重KG
                "cgBmi": BMI
                "cgYqtz": 孕前体重KG
                "cgYqbmi": 孕前BMI
                "cgSsy": 血压-收缩压mmHg 血压-收缩压mmHg
                "cgSzy": 血压-舒张压mmHg 正常范围是≥60mmHg，＜90mmHg
                "cgXl": 心率次/分
                "cgTw": 体温摄氏度
                "cgHx": 呼吸次/分
                "cgFw": 腹围CM
                "cgGg": 宫高CM
                "cgFz": 浮肿
                "cgGs": 宫缩
                "cgXj": 衔接
                "cgRsqyc": 妊娠期异常
                "cgChts": 产后天数
                "cgChccyj": 产后月经开始天数
            } 
    :param ybjc: 一般检查
            {
                "ybZl": 智力
                "ybZlYcms": 智力-异常描述
                "ybJszt": 精神状态
                "ybJsztYcms": 精神状态-异常描述
                "ybWg": 五官
                "ybWgYcms": 五官-异常描述
                "ybTszt": 特殊状态
                "ybTsztYcms": 特殊状态-异常描述
                "ybTsmr": 特殊面容
                "ybTsmrYcms": 特殊面容-异常描述
                "ybPfmf": 皮肤毛发
                "ybPfmfYcms": 皮肤毛发-异常描述
                "ybJzx": 甲状腺
                "ybJzxYcms": 甲状腺-异常描述
                "ybFb": 肺部
                "ybFbYcms": 肺部-异常描述
                "ybXlbq": 心率不齐
                "ybXlbqYcms": 心率不齐-异常描述
                "ybXzzy": 心脏杂音
                "ybXzzyYcms": 心脏杂音-异常描述
                "ybGp": 肝脾
                "ybGpYcms": 肝脾-异常描述
                "ybSzjz": 四肢脊椎
                "ybSzjzYcms": 四肢脊椎-异常描述
                "ybQt": 其他
            }
    :param fkjc: fkjc
            {
                "fkYm": 阴毛
                "fkYmYcms": 阴毛-异常描述
                "fkRf": 乳房
                "fkRfYcms": 乳房-异常描述
                "fkWy": 外阴
                "fkWyYcms": 外阴-异常描述
                "fkYd": 阴道
                "fkYdYcms": 阴道-异常描述
                "fkFmw": 分泌物
                "fkFmwYcms": 分泌物-异常描述
                "fkGj": 宫颈
                "fkGjYcms": 宫颈-异常描述
                "fkScfj": 双侧附件
                "fkScfjYcms": 双侧附件-异常描述
                "fkZg": 子宫
                "fkZgYcms": 子宫-异常描述
                "fkQt": 其他
                "fkEl": 恶露
                "fzElYcms": 恶露-异常描述
                "fkSk": 伤口
                "fkSkYcms": 伤口-异常描述
            }
    :param tejc: 胎儿检查
            [
                {
                    "id": id
                    "tejcTd": 胎动
                    "tejcTw": 胎位
                    "tejcTxl": 胎先露
                    "tejcXl": 胎心率
                    "tejcJh": 胎心监护
                }
            ]
    :param pgjc: 盆骨检查
            {
                "pgPgclZgjjj": 盆骨测量-坐骨棘间经
                "pgPgclQqjj": 髂峭间经CM
                "pgPgclZgjjjj": 骨结节间经CM
                "pgPgclDcwj": 骶耻外经CM
                "pgGpnDj": 骶岬
                "pgGpnDahd": 骶凹弧度
                "pgGpnDwgjhdd": 骶尾关节活动度
                "pgGpnWg": 尾骨
                "pgGpnCgghd": 耻骨弓弧度
                "pgGpnSczgj": 双侧坐骨棘
                "pgGpnZgqjkd": 坐骨切迹宽度CM
                "pgGpnZghjhd": 坐骨后节横度CM
                "pgGjglGkkd": 宫颈开大
                "pgGjglGjgxt": 宫颈管消退
                "pgGjglGjyd": 宫颈硬度
                "pgGjglXlwz": 先露位置
                "pgGjglXlbgd": 先露部高低
                "pgGjglBishops": Bishops评分
            }
    :returns: json string

    1. 新增时 先向体格检查表 插入一条
    2. 然后检查checkType 如果非null 就将其与logId关联
    """
    def post(self):
        try:
            # 保存体格检查
            odoo = OdooManager().get_instance()
            params = tools.get_params(request)
            # 参数校验
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            # 当有checkType时
            if (params.get('checkType')):
                progress_data = check_progress(params.get('archivesId'), params.get('checkType'))
                if not progress_data['data']:
                    return jsonify({
                        'msg': progress_data['msg'],
                        'code': 1
                    })
            data_ = {
                'archives_id': int(params.get('archivesId')),
                #'inspection_log_id': params.get('logId'),
                'type': params.get('checkType'),
                'cg_tjrq': nullablea_dict(params.get('cg'), 'cgTjrq'),
                'cg_sg': nullablea_dict(params.get('cg'), 'cgSg'),
                'cg_tz': nullablea_dict(params.get('cg'), 'cgTz'),
                'cg_bmi': nullablea_dict(params.get('cg'), 'cgBmi'),
                'cg_yqtz': nullablea_dict(params.get('cg'), 'cgYqtz'),
                'cg_yqbmi': nullablea_dict(params.get('cg'), 'cgYqbmi'),
                'cg_ssy': nullablea_dict(params.get('cg'), 'cgSsy'),
                'cg_szy': nullablea_dict(params.get('cg'), 'cgSzy'),
                'cg_xl': nullablea_dict(params.get('cg'), 'cgXl'),
                'cg_tw': nullablea_dict(params.get('cg'), 'cgTw'),
                'cg_hx': nullablea_dict(params.get('cg'), 'cgHx'),
                'cg_fw': nullablea_dict(params.get('cg'), 'cgFw'),
                'cg_gg': nullablea_dict(params.get('cg'), 'cgGg'),
                'cg_fz': nullablea_dict(params.get('cg'), 'cgFz'),
                'cg_gs': nullablea_dict(params.get('cg'), 'cgGs'),
                'cg_xj': nullablea_dict(params.get('cg'), 'cgXj'),
                'cg_rsqyc': nullablea_dict(params.get('cg'), 'cgRsqyc'),
                'cg_chts': nullablea_dict(params.get('cg'), 'cgChts'),
                'cg_chccyj': nullablea_dict(params.get('cg'), 'cgChccyj'),
                'yb_zl': nullablea_dict(params.get('ybjc'), 'ybZl'),
                'yb_zl_ycms': nullablea_dict(params.get('ybjc'), 'ybZlYcms'),
                'yb_jszt': nullablea_dict(params.get('ybjc'), 'ybJszt'),
                'yb_jszt_ycms': nullablea_dict(params.get('ybjc'), 'ybJsztYcms'),
                'yb_wg': nullablea_dict(params.get('ybjc'), 'ybWg'),
                'yb_wg_ycms': nullablea_dict(params.get('ybjc'), 'ybWgYcms'),
                'yb_tszt': nullablea_dict(params.get('ybjc'), 'ybTszt'),
                'yb_tszt_ycms': nullablea_dict(params.get('ybjc'), 'ybTsztYcms'),
                'yb_tsmr': nullablea_dict(params.get('ybjc'), 'ybTsmr'),
                'yb_tsmr_ycms': nullablea_dict(params.get('ybjc'), 'ybTsmrYcms'),
                'yb_pfmf': nullablea_dict(params.get('ybjc'), 'ybPfmf'),
                'yb_pfmf_ycms': nullablea_dict(params.get('ybjc'), 'ybPfmfYcms'),
                'yb_jzx': nullablea_dict(params.get('ybjc'), 'ybJzx'),
                'yb_jzx_ycms': nullablea_dict(params.get('ybjc'), 'ybJzxYcms'),
                'yb_fb': nullablea_dict(params.get('ybjc'), 'ybFb'),
                'yb_fb_ycms': nullablea_dict(params.get('ybjc'), 'ybFbYcms'),
                'yb_xlbq': nullablea_dict(params.get('ybjc'), 'ybXlbq'),
                'yb_xlbq_ycms': nullablea_dict(params.get('ybjc'), 'ybXlbqYcms'),
                'yb_xzzy': nullablea_dict(params.get('ybjc'), 'ybXzzy'),
                'yb_xzzy_ycms': nullablea_dict(params.get('ybjc'), 'ybXzzyYcms'),
                'yb_gp': nullablea_dict(params.get('ybjc'), 'ybGp'),
                'yb_gp_ycms': nullablea_dict(params.get('ybjc'), 'ybGpYcms'),
                'yb_szjz': nullablea_dict(params.get('ybjc'), 'ybSzjz'),
                'yb_szjz_ycms': nullablea_dict(params.get('ybjc'), 'ybSzjzYcms'),
                'yb_qt': nullablea_dict(params.get('ybjc'), 'ybQt'),
                'fk_ym': nullablea_dict(params.get('fkjc'), 'fkYm'),
                'fk_ym_ycms': nullablea_dict(params.get('fkjc'), 'fkYmYcms'),
                'fk_rf': nullablea_dict(params.get('fkjc'), 'fkRf'),
                'fk_rf_ycms': nullablea_dict(params.get('fkjc'), 'fkRfYcms'),
                'fk_wy': nullablea_dict(params.get('fkjc'), 'fkWy'),
                'fk_wy_ycms': nullablea_dict(params.get('fkjc'), 'fkWyYcms'),
                'fk_yd': nullablea_dict(params.get('fkjc'), 'fkYd'),
                'fk_yd_ycms': nullablea_dict(params.get('fkjc'), 'fkYdYcms'),
                'fk_fmw': nullablea_dict(params.get('fkjc'), 'fkFmw'),
                'fk_fmw_ycms': nullablea_dict(params.get('fkjc'), 'fkFmwYcms'),
                'fk_gj': nullablea_dict(params.get('fkjc'), 'fkGj'),
                'fk_gj_ycms': nullablea_dict(params.get('fkjc'), 'fkGjYcms'),
                'fk_scfj': nullablea_dict(params.get('fkjc'), 'fkScfj'),
                'fk_scfj_ycms': nullablea_dict(params.get('fkjc'), 'fkScfjYcms'),
                'fk_zg': nullablea_dict(params.get('fkjc'), 'fkZg'),
                'fk_zg_ycms': nullablea_dict(params.get('fkjc'), 'fkZgYcms'),
                'fk_el': nullablea_dict(params.get('fkjc'), 'fkEl'),
                'fk_el_ycms': nullablea_dict(params.get('fkjc'), 'fzElYcms'),
                'fk_sk': nullablea_dict(params.get('fkjc'), 'fkSk'),
                'fk_sk_ycms': nullablea_dict(params.get('fkjc'), 'fkSkYcms'),
                'fk_qt': nullablea_dict(params.get('fkjc'), 'fkQt'),
                'pg_pgcl_zgjjj': nullablea_dict(params.get('pgjc'), 'pgPgclZgjjj'),
                'pg_pgcl_qqjj': nullablea_dict(params.get('pgjc'), 'pgPgclZgjjj'),
                'pg_pgcl_dcwj': nullablea_dict(params.get('pgjc'), 'pgPgclDcwj'),
                'pg_pgcl_zgjjjj': nullablea_dict(params.get('pgjc'), 'pgPgclZgjjjj'),
                'pg_gpn_dj': nullablea_dict(params.get('pgjc'), 'pgGpnDj'),
                'pg_gpn_dahd': nullablea_dict(params.get('pgjc'), 'pgGpnDahd'),
                'pg_gpn_dwgjhdd': nullablea_dict(params.get('pgjc'), 'pgGpnDwgjhdd'),
                'pg_gpn_wg': nullablea_dict(params.get('pgjc'), 'pgGpnWg'),
                'pg_gpn_cgghd': nullablea_dict(params.get('pgjc'), 'pgGpnCgghd'),
                'pg_gpn_sczgj': nullablea_dict(params.get('pgjc'), 'pgGpnSczgj'),
                'pg_gpn_zgqjkd': nullablea_dict(params.get('pgjc'), 'pgGpnZgqjkd'),
                'pg_gpn_zghjhd': nullablea_dict(params.get('pgjc'), 'pgGpnZghjhd'),
                'pg_gjgl_gkkd': nullablea_dict(params.get('pgjc'), 'pgGjglGkkd'),
                'pg_gjgl_gjgxt': nullablea_dict(params.get('pgjc'), 'pgGjglGjgxt'),
                'pg_gjgl_gjyd': nullablea_dict(params.get('pgjc'), 'pgGjglGjyd'),
                'pg_gjgl_xlwz': nullablea_dict(params.get('pgjc'), 'pgGjglXlwz'),
                'pg_gjgl_xlbgd': nullablea_dict(params.get('pgjc'), 'pgGjglXlbgd'),
                'pg_gjgl_bishops': nullablea_dict(params.get('pgjc'), 'pgGjglBishops'),
            }
            # 先新增一条体格检查记录
            record_id = odoo.env['inspection.physical'].create(data_)
            # 保存胎儿检查
            if params.get('tejc'):
                for tejc_ in params.get('tejc'):
                    te_data_ = {
                        'pid': record_id,
                        'tejc_td': tejc_['tejcTd'],
                        'tejc_xl': tejc_['tejcXl'],
                        'tejc_tw': tejc_['tejcTw'],
                        'tejc_txl': tejc_['tejcTxl'],
                        'tejc_txjh': tejc_['tejcJh'],
                        'hospital_id': 1,
                    }
                    te_data_ = odoo.env['inspection.physical.fetus'].create(te_data_)
            res_data = {}
            # 检查checkType 是否需要与log表关联
            if not params.get('checkType'):
                # 护士工作站 无需插入
                res_data['id'] = record_id
            else:
                # 查询log表 查询该档案的在途情况
                log_data = odoo.env['inspection.log'].search_read([
                    ('archives_id', '=', params.get('archivesId')),
                    ('state', '=', '0'),
                    ('tgjx_id', '=', None)
                ])
                # 如果有在途
                if (log_data and len(log_data) != 0):
                    # 修改log记录 增加体格检查Id
                    odoo.env['inspection.log'].write(log_data[0]['id'], {
                        'tgjx_id': record_id
                    })
                    res_data['logId'] = log_data[0]['id']
                    res_data['id'] = record_id
                else:
                    log_id = odoo.env['inspection.log'].create({
                        'tgjx_id': record_id,
                        'archives_id': params.get('archivesId'),
                        'type': params.get('checkType'),
                        'state': '0'
                    })
                    res_data['logId'] = log_id
                    res_data['id'] = record_id
                # 修改体格检查记录 增加logId
                odoo.env['inspection.physical'].write(record_id, {
                    'inspection_log_id': res_data['logId']
                })
                # 记录操作日志
                create_event = event_type.OperationLogEvent('新增log记录', 'logid:{}'.format(res_data['logId']))
                message_bus = MessageBusManager().get_message_bus()
                message_bus.handle(create_event)  # 发布事件
            # 记录操作日志
            create_event = event_type.OperationLogEvent('新增体格检查', '档案id:{}'.format(params.get('archivesId')))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            return jsonify({'code': 0, 'msg': 'success', 'data': res_data, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class PhyExaUpdateAPI(Resource):
    """
    修改体格检查
    :request: put
    :param archivesId：档案ID
    :param checkType：类型 null 护士工作站 1 初检 2 常规 3 产后
    :param logId：日志id
    :param cg: 常规检查
            {   
                "cgTjrq": 体检日期
                "cgSg": 身高CM
                "cgTz": 体重KG
                "cgBmi": BMI
                "cgYqtz": 孕前体重KG
                "cgYqbmi": 孕前BMI
                "cgSsy": 血压-收缩压mmHg 血压-收缩压mmHg
                "cgSzy": 血压-舒张压mmHg 正常范围是≥60mmHg，＜90mmHg
                "cgXl": 心率次/分
                "cgTw": 体温摄氏度
                "cgHx": 呼吸次/分
                "cgFw": 腹围CM
                "cgGg": 宫高CM
                "cgFz": 浮肿
                "cgGs": 宫缩
                "cgXj": 衔接
                "cgRsqyc": 妊娠期异常
                "cgChts": 产后天数
                "cgChccyj": 产后月经开始天数
            } 
    :param ybjc: 一般检查
            {
                "ybZl": 智力
                "ybZlYcms": 智力-异常描述
                "ybJszt": 精神状态
                "ybJsztYcms": 精神状态-异常描述
                "ybWg": 五官
                "ybWgYcms": 五官-异常描述
                "ybTszt": 特殊状态
                "ybTsztYcms": 特殊状态-异常描述
                "ybTsmr": 特殊面容
                "ybTsmrYcms": 特殊面容-异常描述
                "ybPfmf": 皮肤毛发
                "ybPfmfYcms": 皮肤毛发-异常描述
                "ybJzx": 甲状腺
                "ybJzxYcms": 甲状腺-异常描述
                "ybFb": 肺部
                "ybFbYcms": 肺部-异常描述
                "ybXlbq": 心率不齐
                "ybXlbqYcms": 心率不齐-异常描述
                "ybXzzy": 心脏杂音
                "ybXzzyYcms": 心脏杂音-异常描述
                "ybGp": 肝脾
                "ybGpYcms": 肝脾-异常描述
                "ybSzjz": 四肢脊椎
                "ybSzjzYcms": 四肢脊椎-异常描述
                "ybQt": 其他
            }
    :param fkjc: fkjc
            {
                "fkYm": 阴毛
                "fkYmYcms": 阴毛-异常描述
                "fkRf": 乳房
                "fkRfYcms": 乳房-异常描述
                "fkWy": 外阴
                "fkWyYcms": 外阴-异常描述
                "fkYd": 阴道
                "fkYdYcms": 阴道-异常描述
                "fkFmw": 分泌物
                "fkFmwYcms": 分泌物-异常描述
                "fkGj": 宫颈
                "fkGjYcms": 宫颈-异常描述
                "fkScfj": 双侧附件
                "fkScfjYcms": 双侧附件-异常描述
                "fkZg": 子宫
                "fkZgYcms": 子宫-异常描述
                "fkQt": 其他
                "fkEl": 恶露
                "fzElYcms": 恶露-异常描述
                "fkSk": 伤口
                "fkSkYcms": 伤口-异常描述
            }
    :param tejc: 胎儿检查
            [
                {
                    "id": id
                    "tejcTd": 胎动
                    "tejcTw": 胎位
                    "tejcTxl": 胎先露
                    "tejcXl": 胎心率
                    "tejcJh": 胎心监护
                }
            ]
    :param pgjc: 盆骨检查
            {
                "pgPgclZgjjj": 盆骨测量-坐骨棘间经
                "pgPgclQqjj": 髂峭间经CM
                "pgPgclZgjjjj": 骨结节间经CM
                "pgPgclDcwj": 骶耻外经CM
                "pgGpnDj": 骶岬
                "pgGpnDahd": 骶凹弧度
                "pgGpnDwgjhdd": 骶尾关节活动度
                "pgGpnWg": 尾骨
                "pgGpnCgghd": 耻骨弓弧度
                "pgGpnSczgj": 双侧坐骨棘
                "pgGpnZgqjkd": 坐骨切迹宽度CM
                "pgGpnZghjhd": 坐骨后节横度CM
                "pgGjglGkkd": 宫颈开大
                "pgGjglGjgxt": 宫颈管消退
                "pgGjglGjyd": 宫颈硬度
                "pgGjglXlwz": 先露位置
                "pgGjglXlbgd": 先露部高低
                "pgGjglBishops": Bishops评分
            }
    :returns: json string
    1. 通过id更新数据
    """
    def put(self):
        try:
            params = request.json
            # 保存体格检查
            odoo = OdooManager().get_instance()
            # 参数校验
            req_params = ['archivesId', 'id']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            inspection_physical = odoo.env['inspection.physical'].search([
                ('archives_id', '=', int(params.get('archivesId'))),
                ('id', '=', int(params.get('id')))
            ])
            if not inspection_physical:
                return jsonify({'code': 40005, 'msg': error_code[40005]})     
            data_ = {
                'archives_id': int(params.get('archivesId')),
                # 'inspection_log_id': params.get('logId'),
                'type': params.get('checkType'),
                'cg_tjrq': nullablea_dict(params.get('cg'), 'cgTjrq'),
                'cg_sg': nullablea_dict(params.get('cg'), 'cgSg'),
                'cg_tz': nullablea_dict(params.get('cg'), 'cgTz'),
                'cg_bmi': nullablea_dict(params.get('cg'), 'cgBmi'),
                'cg_yqtz': nullablea_dict(params.get('cg'), 'cgYqtz'),
                'cg_yqbmi': nullablea_dict(params.get('cg'), 'cgYqbmi'),
                'cg_ssy': nullablea_dict(params.get('cg'), 'cgSsy'),
                'cg_szy': nullablea_dict(params.get('cg'), 'cgSzy'),
                'cg_xl': nullablea_dict(params.get('cg'), 'cgXl'),
                'cg_tw': nullablea_dict(params.get('cg'), 'cgTw'),
                'cg_hx': nullablea_dict(params.get('cg'), 'cgHx'),
                'cg_fw': nullablea_dict(params.get('cg'), 'cgFw'),
                'cg_gg': nullablea_dict(params.get('cg'), 'cgGg'),
                'cg_fz': nullablea_dict(params.get('cg'), 'cgFz'),
                'cg_gs': nullablea_dict(params.get('cg'), 'cgGs'),
                'cg_xj': nullablea_dict(params.get('cg'), 'cgXj'),
                'cg_rsqyc': nullablea_dict(params.get('cg'), 'cgRsqyc'),
                'cg_chts': nullablea_dict(params.get('cg'), 'cgChts'),
                'cg_chccyj': nullablea_dict(params.get('cg'), 'cgChccyj'),
                'yb_zl': nullablea_dict(params.get('ybjc'), 'ybZl'),
                'yb_zl_ycms': nullablea_dict(params.get('ybjc'), 'ybZlYcms'),
                'yb_jszt': nullablea_dict(params.get('ybjc'), 'ybJszt'),
                'yb_jszt_ycms': nullablea_dict(params.get('ybjc'), 'ybJsztYcms'),
                'yb_wg': nullablea_dict(params.get('ybjc'), 'ybWg'),
                'yb_wg_ycms': nullablea_dict(params.get('ybjc'), 'ybWgYcms'),
                'yb_tszt': nullablea_dict(params.get('ybjc'), 'ybTszt'),
                'yb_tszt_ycms': nullablea_dict(params.get('ybjc'), 'ybTsztYcms'),
                'yb_tsmr': nullablea_dict(params.get('ybjc'), 'ybTsmr'),
                'yb_tsmr_ycms': nullablea_dict(params.get('ybjc'), 'ybTsmrYcms'),
                'yb_pfmf': nullablea_dict(params.get('ybjc'), 'ybPfmf'),
                'yb_pfmf_ycms': nullablea_dict(params.get('ybjc'), 'ybPfmfYcms'),
                'yb_jzx': nullablea_dict(params.get('ybjc'), 'ybJzx'),
                'yb_jzx_ycms': nullablea_dict(params.get('ybjc'), 'ybJzxYcms'),
                'yb_fb': nullablea_dict(params.get('ybjc'), 'ybFb'),
                'yb_fb_ycms': nullablea_dict(params.get('ybjc'), 'ybFbYcms'),
                'yb_xlbq': nullablea_dict(params.get('ybjc'), 'ybXlbq'),
                'yb_xlbq_ycms': nullablea_dict(params.get('ybjc'), 'ybXlbqYcms'),
                'yb_xzzy': nullablea_dict(params.get('ybjc'), 'ybXzzy'),
                'yb_xzzy_ycms': nullablea_dict(params.get('ybjc'), 'ybXzzyYcms'),
                'yb_gp': nullablea_dict(params.get('ybjc'), 'ybGp'),
                'yb_gp_ycms': nullablea_dict(params.get('ybjc'), 'ybGpYcms'),
                'yb_szjz': nullablea_dict(params.get('ybjc'), 'ybSzjz'),
                'yb_szjz_ycms': nullablea_dict(params.get('ybjc'), 'ybSzjzYcms'),
                'yb_qt': nullablea_dict(params.get('ybjc'), 'ybQt'),
                'fk_ym': nullablea_dict(params.get('fkjc'), 'fkYm'),
                'fk_ym_ycms': nullablea_dict(params.get('fkjc'), 'fkYmYcms'),
                'fk_rf': nullablea_dict(params.get('fkjc'), 'fkRf'),
                'fk_rf_ycms': nullablea_dict(params.get('fkjc'), 'fkRfYcms'),
                'fk_wy': nullablea_dict(params.get('fkjc'), 'fkWy'),
                'fk_wy_ycms': nullablea_dict(params.get('fkjc'), 'fkWyYcms'),
                'fk_yd': nullablea_dict(params.get('fkjc'), 'fkYd'),
                'fk_yd_ycms': nullablea_dict(params.get('fkjc'), 'fkYdYcms'),
                'fk_fmw': nullablea_dict(params.get('fkjc'), 'fkFmw'),
                'fk_fmw_ycms': nullablea_dict(params.get('fkjc'), 'fkFmwYcms'),
                'fk_gj': nullablea_dict(params.get('fkjc'), 'fkGj'),
                'fk_gj_ycms': nullablea_dict(params.get('fkjc'), 'fkGjYcms'),
                'fk_scfj': nullablea_dict(params.get('fkjc'), 'fkScfj'),
                'fk_scfj_ycms': nullablea_dict(params.get('fkjc'), 'fkScfjYcms'),
                'fk_zg': nullablea_dict(params.get('fkjc'), 'fkZg'),
                'fk_zg_ycms': nullablea_dict(params.get('fkjc'), 'fkZgYcms'),
                'fk_el': nullablea_dict(params.get('fkjc'), 'fkEl'),
                'fk_el_ycms': nullablea_dict(params.get('fkjc'), 'fzElYcms'),
                'fk_sk': nullablea_dict(params.get('fkjc'), 'fkSk'),
                'fk_sk_ycms': nullablea_dict(params.get('fkjc'), 'fkSkYcms'),
                'fk_qt': nullablea_dict(params.get('fkjc'), 'fkQt'),
                'pg_pgcl_zgjjj': nullablea_dict(params.get('pgjc'), 'pgPgclZgjjj'),
                'pg_pgcl_qqjj': nullablea_dict(params.get('pgjc'), 'pgPgclZgjjj'),
                'pg_pgcl_dcwj': nullablea_dict(params.get('pgjc'), 'pgPgclDcwj'),
                'pg_pgcl_zgjjjj': nullablea_dict(params.get('pgjc'), 'pgPgclZgjjjj'),
                'pg_gpn_dj': nullablea_dict(params.get('pgjc'), 'pgGpnDj'),
                'pg_gpn_dahd': nullablea_dict(params.get('pgjc'), 'pgGpnDahd'),
                'pg_gpn_dwgjhdd': nullablea_dict(params.get('pgjc'), 'pgGpnDwgjhdd'),
                'pg_gpn_wg': nullablea_dict(params.get('pgjc'), 'pgGpnWg'),
                'pg_gpn_cgghd': nullablea_dict(params.get('pgjc'), 'pgGpnCgghd'),
                'pg_gpn_sczgj': nullablea_dict(params.get('pgjc'), 'pgGpnSczgj'),
                'pg_gpn_zgqjkd': nullablea_dict(params.get('pgjc'), 'pgGpnZgqjkd'),
                'pg_gpn_zghjhd': nullablea_dict(params.get('pgjc'), 'pgGpnZghjhd'),
                'pg_gjgl_gkkd': nullablea_dict(params.get('pgjc'), 'pgGjglGkkd'),
                'pg_gjgl_gjgxt': nullablea_dict(params.get('pgjc'), 'pgGjglGjgxt'),
                'pg_gjgl_gjyd': nullablea_dict(params.get('pgjc'), 'pgGjglGjyd'),
                'pg_gjgl_xlwz': nullablea_dict(params.get('pgjc'), 'pgGjglXlwz'),
                'pg_gjgl_xlbgd': nullablea_dict(params.get('pgjc'), 'pgGjglXlbgd'),
                'pg_gjgl_bishops': nullablea_dict(params.get('pgjc'), 'pgGjglBishops'),
            } 									
            odoo.env['inspection.physical'].write(inspection_physical,data_)
            # 保存胎儿检查
            if params.get('tejc'):
                query_ = [('pid', '=', int(params.get('id')))]
                inspection_physical_fetus = odoo.env['inspection.physical.fetus'].search(query_)
                if inspection_physical_fetus:
                    # 这里需要先删除旧的胎儿 然后产生新的胎儿
                    odoo.env['inspection.physical.fetus'].unlink(inspection_physical_fetus)
                for tejc_ in params.get('tejc'):
                    te_data_ = {
                        # 'id': params.get('id'),
                        'pid': inspection_physical[0],
                        'tejc_td': tejc_['tejcTd'],
                        'tejc_xl': tejc_['tejcXl'],
                        'tejc_tw': tejc_['tejcTw'],
                        'tejc_txl': tejc_['tejcTxl'],
                        'tejc_txjh': tejc_['tejcJh'],
                        'hospital_id': 1,
                    }
                    te_data_ = odoo.env['inspection.physical.fetus'].create(te_data_)
            if not params.get('history'):
                check_physical(params.get('archivesId'), params.get('checkType'))
            # 记录操作日志
            create_event = event_type.OperationLogEvent('修改体格检查', '档案id:{}'.format(params.get('archivesId')))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            return jsonify({'code': 0, 'msg': 'success', 'data': {'logId': params.get('logId'), 'id': inspection_physical}, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class PhyExaQueryByArchivesIdAPI(Resource):
    """
    查询体格检查 护士站做完体格检查，回显使用
    :request: get
    :param archivesId: 档案ID
    :param checkType: 检查类型 null-护士工作站 1-初检 2-常规 3-产后
    :returns: json string

    步骤：
    1. 查询log表 如果该记录 在途且有体格检查 则返回
    2. 然后查询体格检查表 无logId的记录
    """
    def get(self):
        try:
            #  参数校验
            params = request.values
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)  
            odoo = OdooManager().get_instance()
            res_data = {}
            query_data = {}
            log_item = odoo.env['inspection.log'].search_read([('archives_id', '=', int(params.get('archivesId'))), ('state', '=', '0')])
            # log表对应的有体格检查
            if (log_item and log_item[0]['tgjx_id']):
                query_data = odoo.env['inspection.physical'].search_read([
                    ('archives_id', '=', int(params.get('archivesId'))),
                    ('id', '=', log_item[0]['tgjx_id'][0])
                ])
            # log表无对应体格检查 则直接查体格检查表
            else:
                query_data = odoo.env['inspection.physical'].search_read([
                    ('archives_id', '=', int(params.get('archivesId'))),
                    ('inspection_log_id', '=', None),
                ])
            if query_data:
                inspection_physical_ = query_data[0]
                res_data = {
                    'cg': {
                        'cgTjrq': inspection_physical_['cg_tjrq'],
                        'cgSg': inspection_physical_['cg_sg'],
                        'cgTz': inspection_physical_['cg_tz'],
                        'cgBmi': inspection_physical_['cg_bmi'],
                        'cgYqtz': inspection_physical_['cg_yqtz'],
                        'cgYqbmi': inspection_physical_['cg_yqbmi'],
                        'cgSsy': inspection_physical_['cg_ssy'],
                        'cgSzy': inspection_physical_['cg_szy'],
                        'cgXl': inspection_physical_['cg_xl'],
                        'cgTw': inspection_physical_['cg_tw'],
                        'cgHx': inspection_physical_['cg_hx'],
                        'cgFw': inspection_physical_['cg_fw'],
                        'cgGg': inspection_physical_['cg_gg'],
                        'cgFz': inspection_physical_['cg_fz'],
                        'cgGs': inspection_physical_['cg_gs'],
                        'cgXj': inspection_physical_['cg_xj'],
                        'cgRsqyc': inspection_physical_['cg_rsqyc'],
                        'cgChts': inspection_physical_['cg_chts'],
                        'cgChccyj': inspection_physical_['cg_chccyj']   
                    },
                    'ybjc': {
                        'ybZl': inspection_physical_['yb_zl'],
                        'ybZlYcms': inspection_physical_['yb_zl_ycms'],
                        'ybJszt': inspection_physical_['yb_jszt'],
                        'ybJsztYcms': inspection_physical_['yb_jszt_ycms'],
                        'ybWg': inspection_physical_['yb_wg'],
                        'ybWgYcms': inspection_physical_['yb_wg_ycms'],
                        'ybTszt': inspection_physical_['yb_tszt'],
                        'ybTsztYcms': inspection_physical_['yb_tszt_ycms'],
                        'ybTsmr': inspection_physical_['yb_tsmr'],
                        'ybTsmrYcms': inspection_physical_['yb_tsmr_ycms'],
                        'ybPfmf': inspection_physical_['yb_pfmf'],
                        'ybPfmfYcms': inspection_physical_['yb_pfmf_ycms'],
                        'ybJzx': inspection_physical_['yb_jzx'],
                        'ybJzxYcms': inspection_physical_['yb_jzx_ycms'],
                        'ybFb': inspection_physical_['yb_fb'],
                        'ybFbYcms': inspection_physical_['yb_fb_ycms'],
                        'ybXlbq': inspection_physical_['yb_xlbq'],
                        'ybXlbqYcms': inspection_physical_['yb_xlbq_ycms'],
                        'ybXzzy': inspection_physical_['yb_xzzy'],
                        'ybXzzyYcms': inspection_physical_['yb_xzzy_ycms'],
                        'ybGp': inspection_physical_['yb_gp'],
                        'ybGpYcms': inspection_physical_['yb_gp_ycms'],
                        'ybSzjz': inspection_physical_['yb_szjz'],
                        'ybSzjzYcms': inspection_physical_['yb_szjz_ycms'],
                        'ybQt': inspection_physical_['yb_qt']
                    },
                    'fkjc': {
                        'fkYm': inspection_physical_['fk_ym'],
                        'fkYmYcms': inspection_physical_['fk_ym_ycms'],
                        'fkRf': inspection_physical_['fk_rf'],
                        'fkRfYcms': inspection_physical_['fk_rf_ycms'],
                        'fkWy': inspection_physical_['fk_wy'],
                        'fkWyYcms': inspection_physical_['fk_wy_ycms'],
                        'fkYd': inspection_physical_['fk_yd'],
                        'fkYdYcms': inspection_physical_['fk_yd_ycms'],
                        'fkFmw': inspection_physical_['fk_fmw'],
                        'fkFmwYcms': inspection_physical_['fk_fmw_ycms'],
                        'fkGj': inspection_physical_['fk_gj'],
                        'fkGjYcms': inspection_physical_['fk_gj_ycms'],
                        'fkScfj': inspection_physical_['fk_scfj'],
                        'fkScfjYcms': inspection_physical_['fk_scfj_ycms'],
                        'fkZg': inspection_physical_['fk_zg'],
                        'fkZgYcms': inspection_physical_['fk_zg_ycms'],
                        'fkQt': inspection_physical_['fk_qt'],
                        'fkEl': inspection_physical_['fk_el'],
                        'fzElYcms': inspection_physical_['fk_el_ycms'],
                        'fkSk': inspection_physical_['fk_sk'],
                        'fkSkYcms': inspection_physical_['fk_sk_ycms']
                    },
                    'pgjc': {
                        'pgPgclZgjjj': inspection_physical_['pg_pgcl_zgjjj'],
                        'pgPgclQqjj': inspection_physical_['pg_pgcl_qqjj'],
                        'pgPgclZgjjjj': inspection_physical_['pg_pgcl_zgjjjj'],
                        'pgPgclDcwj': inspection_physical_['pg_pgcl_dcwj'],
                        'pgGpnDj': inspection_physical_['pg_gpn_dj'],
                        'pgGpnDahd': inspection_physical_['pg_gpn_dahd'],
                        'pgGpnDwgjhdd': inspection_physical_['pg_gpn_dwgjhdd'],
                        'pgGpnWg': inspection_physical_['pg_gpn_wg'],
                        'pgGpnCgghd': inspection_physical_['pg_gpn_cgghd'],
                        'pgGpnSczgj': inspection_physical_['pg_gpn_sczgj'],
                        'pgGpnZgqjkd': inspection_physical_['pg_gpn_zgqjkd'],
                        'pgGpnZghjhd': inspection_physical_['pg_gpn_zghjhd'],
                        'pgGjglGkkd': inspection_physical_['pg_gjgl_gkkd'],
                        'pgGjglGjgxt': inspection_physical_['pg_gjgl_gjgxt'],
                        'pgGjglGjyd': inspection_physical_['pg_gjgl_gjyd'],
                        'pgGjglXlwz': inspection_physical_['pg_gjgl_xlwz'],
                        'pgGjglXlbgd': inspection_physical_['pg_gjgl_xlbgd'],
                        'pgGjglBishops': inspection_physical_['pg_gjgl_bishops']
                    },
                    'tejc': [],
                    'logId': inspection_physical_['inspection_log_id'][0] if inspection_physical_['inspection_log_id'] else None,
                    'id': inspection_physical_['id']
                }
                #  获取胎儿检查
                query_ = [('pid', '=', inspection_physical_['id'])]
                inspection_physical_fetus = odoo.env['inspection.physical.fetus'].search_read(query_)
                if inspection_physical_fetus:
                    for inspection_physical_fetus_ in inspection_physical_fetus:
                        ter_data_ = {
                            'id': inspection_physical_fetus_['id'],
                            'tejcTd': inspection_physical_fetus_['tejc_td'],
                            'tejcTw': inspection_physical_fetus_['tejc_tw'],
                            'tejcTxl': inspection_physical_fetus_['tejc_txl'],
                            'tejcXl': inspection_physical_fetus_['tejc_xl'],
                            'tejcJh': inspection_physical_fetus_['tejc_txjh']
                        }
                        res_data['tejc'].append(ter_data_)
            return jsonify({'code': 0, 'msg': 'success', 'data': res_data, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class PhyExaHistoryAPI(Resource):
    """
    根据logId和档案Id查询体格检查历史记录
    :request: post
    :param archivesId: 档案ID
    :param logId: logId
    :param dataSource: 数据来源 null 全部 0 本院 1外院
    :returns: json string
    """
    def get(self):
        try:

            odoo = OdooManager().get_instance()
            params = request.values
            # 参数校验
            req_params = ['archivesId','logId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            query_ = [
                ('archives_id', '=', int(params.get('archivesId'))),
                ('inspection_log_id', '=', int(params.get('logId')))
            ]
            data_ = {}
            inspection_physical = odoo.env['inspection.physical'].search_read(query_)
            if inspection_physical:
                inspection_physical_ = inspection_physical.pop()
                data_ = {
                    'cg': {
                        'cgTjrq': inspection_physical_['cg_tjrq'],
                        'cgSg': inspection_physical_['cg_sg'],
                        'cgTz': inspection_physical_['cg_tz'],
                        'cgBmi': inspection_physical_['cg_bmi'],
                        'cgYqtz': inspection_physical_['cg_yqtz'],
                        'cgYqbmi': inspection_physical_['cg_yqbmi'],
                        'cgSsy': inspection_physical_['cg_ssy'],
                        'cgSzy': inspection_physical_['cg_szy'],
                        'cgXl': inspection_physical_['cg_xl'],
                        'cgTw': inspection_physical_['cg_tw'],
                        'cgHx': inspection_physical_['cg_hx'],
                        'cgFw': inspection_physical_['cg_fw'],
                        'cgGg': inspection_physical_['cg_gg'],
                        'cgFz': inspection_physical_['cg_fz'],
                        'cgGs': inspection_physical_['cg_gs'],
                        'cgXj': inspection_physical_['cg_xj'],
                        'cgRsqyc': inspection_physical_['cg_rsqyc'],
                        'cgChts': inspection_physical_['cg_chts'],
                        'cgChccyj': inspection_physical_['cg_chccyj']   
                    },
                    'ybjc': {
                        'ybZl': inspection_physical_['yb_zl'],
                        'ybZlYcms': inspection_physical_['yb_zl_ycms'],
                        'ybJszt': inspection_physical_['yb_jszt'],
                        'ybJsztYcms': inspection_physical_['yb_jszt_ycms'],
                        'ybWg': inspection_physical_['yb_wg'],
                        'ybWgYcms': inspection_physical_['yb_wg_ycms'],
                        'ybTszt': inspection_physical_['yb_tszt'],
                        'ybTsztYcms': inspection_physical_['yb_tszt_ycms'],
                        'ybTsmr': inspection_physical_['yb_tsmr'],
                        'ybTsmrYcms': inspection_physical_['yb_tsmr_ycms'],
                        'ybPfmf': inspection_physical_['yb_pfmf'],
                        'ybPfmfYcms': inspection_physical_['yb_pfmf_ycms'],
                        'ybJzx': inspection_physical_['yb_jzx'],
                        'ybJzxYcms': inspection_physical_['yb_jzx_ycms'],
                        'ybFb': inspection_physical_['yb_fb'],
                        'ybFbYcms': inspection_physical_['yb_fb_ycms'],
                        'ybXlbq': inspection_physical_['yb_xlbq'],
                        'ybXlbqYcms': inspection_physical_['yb_xlbq_ycms'],
                        'ybXzzy': inspection_physical_['yb_xzzy'],
                        'ybXzzyYcms': inspection_physical_['yb_xzzy_ycms'],
                        'ybGp': inspection_physical_['yb_gp'],
                        'ybGpYcms': inspection_physical_['yb_gp_ycms'],
                        'ybSzjz': inspection_physical_['yb_szjz'],
                        'ybSzjzYcms': inspection_physical_['yb_szjz_ycms'],
                        'ybQt': inspection_physical_['yb_qt']
                    },
                    'fkjc': {
                        'fkYm': inspection_physical_['fk_ym'],
                        'fkYmYcms': inspection_physical_['fk_ym_ycms'],
                        'fkRf': inspection_physical_['fk_rf'],
                        'fkRfYcms': inspection_physical_['fk_rf_ycms'],
                        'fkWy': inspection_physical_['fk_wy'],
                        'fkWyYcms': inspection_physical_['fk_wy_ycms'],
                        'fkYd': inspection_physical_['fk_yd'],
                        'fkYdYcms': inspection_physical_['fk_yd_ycms'],
                        'fkFmw': inspection_physical_['fk_fmw'],
                        'fkFmwYcms': inspection_physical_['fk_fmw_ycms'],
                        'fkGj': inspection_physical_['fk_gj'],
                        'fkGjYcms': inspection_physical_['fk_gj_ycms'],
                        'fkScfj': inspection_physical_['fk_scfj'],
                        'fkScfjYcms': inspection_physical_['fk_scfj_ycms'],
                        'fkZg': inspection_physical_['fk_zg'],
                        'fkZgYcms': inspection_physical_['fk_zg_ycms'],
                        'fkQt': inspection_physical_['fk_qt'],
                        'fkEl': inspection_physical_['fk_el'],
                        'fzElYcms': inspection_physical_['fk_el_ycms'],
                        'fkSk': inspection_physical_['fk_sk'],
                        'fkSkYcms': inspection_physical_['fk_sk_ycms']
                    },
                    'pgjc': {
                        'pgPgclZgjjj': inspection_physical_['pg_pgcl_zgjjj'],
                        'pgPgclQqjj': inspection_physical_['pg_pgcl_qqjj'],
                        'pgPgclZgjjjj': inspection_physical_['pg_pgcl_zgjjjj'],
                        'pgPgclDcwj': inspection_physical_['pg_pgcl_dcwj'],
                        'pgGpnDj': inspection_physical_['pg_gpn_dj'],
                        'pgGpnDahd': inspection_physical_['pg_gpn_dahd'],
                        'pgGpnDwgjhdd': inspection_physical_['pg_gpn_dwgjhdd'],
                        'pgGpnWg': inspection_physical_['pg_gpn_wg'],
                        'pgGpnCgghd': inspection_physical_['pg_gpn_cgghd'],
                        'pgGpnSczgj': inspection_physical_['pg_gpn_sczgj'],
                        'pgGpnZgqjkd': inspection_physical_['pg_gpn_zgqjkd'],
                        'pgGpnZghjhd': inspection_physical_['pg_gpn_zghjhd'],
                        'pgGjglGkkd': inspection_physical_['pg_gjgl_gkkd'],
                        'pgGjglGjgxt': inspection_physical_['pg_gjgl_gjgxt'],
                        'pgGjglGjyd': inspection_physical_['pg_gjgl_gjyd'],
                        'pgGjglXlwz': inspection_physical_['pg_gjgl_xlwz'],
                        'pgGjglXlbgd': inspection_physical_['pg_gjgl_xlbgd'],
                        'pgGjglBishops': inspection_physical_['pg_gjgl_bishops']
                    },
                    'tejc': [],
                    'logId': inspection_physical_['inspection_log_id'][0],
                    'id': inspection_physical_['id']
                }
                #  获取胎儿检查
                query_ = [('pid', '=', inspection_physical_['id'])]
                inspection_physical_fetus = odoo.env['inspection.physical.fetus'].search_read(query_)
                if inspection_physical_fetus:
                    for inspection_physical_fetus_ in inspection_physical_fetus:
                        ter_data_ = {
                            'id': inspection_physical_fetus_['id'],
                            'tejcTd': inspection_physical_fetus_['tejc_td'],
                            'tejcTw': inspection_physical_fetus_['tejc_tw'],
                            'tejcTxl': inspection_physical_fetus_['tejc_txl'],
                            'tejcXl': inspection_physical_fetus_['tejc_xl'],
                            'tejcJh': inspection_physical_fetus_['tejc_txjh']
                        }
                        data_['tejc'].append(ter_data_)
            return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})
